home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap17 / UniChars / Unichars.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.9 KB  |  155 lines

  1. /*-----------------------------------------------
  2.    UNICHARS.C -- Displays 16-bit character codes
  3.                  (c) Charles Petzold, 1998
  4.   -----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  12.                     PSTR szCmdLine, int iCmdShow)
  13. {
  14.      static TCHAR szAppName[] = TEXT ("UniChars") ;
  15.      HWND         hwnd ;
  16.      MSG          msg ;
  17.      WNDCLASS     wndclass ;
  18.      
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = szAppName ;
  28.      wndclass.lpszClassName = szAppName ;
  29.  
  30.      if (!RegisterClass (&wndclass))
  31.      {
  32.           MessageBox (NULL, TEXT ("This program requies Windows NT!"), 
  33.                       szAppName, MB_ICONERROR) ;
  34.           return 0 ;
  35.      }
  36.      
  37.      hwnd = CreateWindow (szAppName, TEXT ("Unicode Characters"),
  38.                           WS_OVERLAPPEDWINDOW | WS_VSCROLL,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           NULL, NULL, hInstance, NULL) ;
  42.      
  43.      ShowWindow (hwnd, iCmdShow) ;
  44.      UpdateWindow (hwnd) ;
  45.      
  46.      while (GetMessage (&msg, NULL, 0, 0))
  47.      {
  48.           TranslateMessage (&msg) ;
  49.           DispatchMessage (&msg) ;
  50.      }
  51.      return msg.wParam ;
  52. }
  53.  
  54. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  55. {
  56.      static CHOOSEFONT cf ;
  57.      static int        iPage ;
  58.      static LOGFONT    lf ;
  59.      HDC               hdc ;
  60.      int               cxChar, cyChar, x, y, i, cxLabels ;
  61.      PAINTSTRUCT       ps ;
  62.      SIZE              size ;
  63.      TCHAR             szBuffer [8] ;
  64.      TEXTMETRIC        tm ;
  65.      WCHAR             ch ;
  66.  
  67.      switch (message)
  68.      {
  69.      case WM_CREATE:
  70.           hdc = GetDC (hwnd) ;
  71.           lf.lfHeight = - GetDeviceCaps (hdc, LOGPIXELSY) / 6 ;  // 12 points
  72.           lstrcpy (lf.lfFaceName, TEXT ("Lucida Sans Unicode")) ;
  73.           ReleaseDC (hwnd, hdc) ;
  74.  
  75.           cf.lStructSize = sizeof (CHOOSEFONT) ;
  76.           cf.hwndOwner   = hwnd ;
  77.           cf.lpLogFont   = &lf ;
  78.           cf.Flags       = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS ;
  79.  
  80.           SetScrollRange (hwnd, SB_VERT, 0, 255, FALSE) ;
  81.           SetScrollPos   (hwnd, SB_VERT, iPage,  TRUE ) ;
  82.           return 0 ;
  83.  
  84.      case WM_COMMAND:
  85.           switch (LOWORD (wParam))
  86.           {
  87.           case IDM_FONT:
  88.                if (ChooseFont (&cf))
  89.                     InvalidateRect (hwnd, NULL, TRUE) ;
  90.                return 0 ;
  91.           }
  92.           return 0 ;
  93.  
  94.      case WM_VSCROLL:
  95.           switch (LOWORD (wParam))
  96.                {
  97.                case SB_LINEUP:         iPage -=  1 ;  break ;
  98.                case SB_LINEDOWN:       iPage +=  1 ;  break ;
  99.                case SB_PAGEUP:         iPage -= 16 ;  break ;
  100.                case SB_PAGEDOWN:       iPage += 16 ;  break ;
  101.                case SB_THUMBPOSITION:  iPage = HIWORD (wParam) ;  break ;
  102.  
  103.                default:
  104.                     return 0 ;
  105.                }
  106.  
  107.           iPage = max (0, min (iPage, 255)) ;
  108.  
  109.           SetScrollPos (hwnd, SB_VERT, iPage, TRUE) ;
  110.           InvalidateRect (hwnd, NULL, TRUE) ;
  111.           return 0 ;
  112.  
  113.      case WM_PAINT:
  114.           hdc = BeginPaint (hwnd, &ps) ;
  115.  
  116.           SelectObject (hdc, CreateFontIndirect (&lf)) ;
  117.  
  118.           GetTextMetrics (hdc, &tm) ;
  119.           cxChar = tm.tmMaxCharWidth ;
  120.           cyChar = tm.tmHeight + tm.tmExternalLeading ;
  121.  
  122.           cxLabels = 0 ;
  123.  
  124.           for (i = 0 ; i < 16 ; i++)
  125.           {
  126.                wsprintf (szBuffer, TEXT (" 000%1X: "), i) ;
  127.                GetTextExtentPoint (hdc, szBuffer, 7, &size) ;
  128.  
  129.                cxLabels = max (cxLabels, size.cx) ;
  130.           }
  131.  
  132.           for (y = 0 ; y < 16 ; y++)
  133.           {
  134.                wsprintf (szBuffer, TEXT (" %03X_: "), 16 * iPage + y) ;
  135.                TextOut (hdc, 0, y * cyChar, szBuffer, 7) ;
  136.  
  137.                for (x = 0 ; x < 16 ; x++)
  138.                {
  139.                     ch = (WCHAR) (256 * iPage + 16 * y + x) ;
  140.                     TextOutW (hdc, x * cxChar + cxLabels,
  141.                                    y * cyChar, &ch, 1);
  142.                }
  143.           }
  144.  
  145.           DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT)));
  146.           EndPaint (hwnd, &ps) ;
  147.           return 0 ;
  148.  
  149.      case WM_DESTROY:
  150.           PostQuitMessage (0) ;
  151.           return 0 ;
  152.      }
  153.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  154. }
  155.